package com.his.controller;

import com.his.annotation.RequireRoles;
import com.his.common.Const;
import com.his.common.PageParam;
import com.his.common.Rsp;
import com.his.entity.ApplyOut;
import com.his.service.ApplyOutService;
import com.his.utils.MyUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (ApplyOut)表控制层
 *
 * @author makejava
 * @since 2020-10-30 13:19:03
 */
@RestController
@RequestMapping("applyOut")
public class ApplyOutController {
    /**
     * 服务对象
     */
    @Resource
    private ApplyOutService applyOutService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public ApplyOut selectOne(Integer id) {
        return this.applyOutService.queryById(id);
    }


    /**
     * 查询所有的病人申请出院信息
     *
     * @param token 令牌
     * @return 病人数据
     */
    @PostMapping("getAll")
    @RequireRoles(roles = {"doctor", "nurse","admin"}, index = 0)
    public Rsp getAll(@RequestHeader(Const.TOKEN) String token, @RequestBody PageParam<ApplyOut> pageParam) throws Exception {
        return applyOutService.queryAll(pageParam,MyUtil.getUser(token));
    }

    /**
     * 添加出院信息
     *
     * @param token    令牌
     * @param applyOut 出院信息
     * @return 返回状态
     */
    @PostMapping("add")
    @RequireRoles(roles = {"doctor"}, index = 0)
    public Rsp add(@RequestHeader(Const.TOKEN) String token, @Validated @RequestBody ApplyOut applyOut) throws Exception {
        applyOut.setCreateUserId(MyUtil.getUserByToken(token));
        return applyOutService.insert(applyOut);
    }

    /**
     * 更改出院状态
     *
     * @param token 令牌
     * @param out    住院
     * @return 返回状态
     */
    @PostMapping("check")
    @RequireRoles(roles = {"nurse"}, index = 0)
    public Rsp check(@RequestHeader(Const.TOKEN) String token, @RequestBody ApplyOut out) throws Exception {
        out.setStatus("1");
        out.setCheckUserId(MyUtil.getUserByToken(token));
        return applyOutService.update(out);
    }

    @PostMapping("update")
    @RequireRoles(roles = {"doctor"}, index = 0)
    public Rsp update(@RequestHeader(Const.TOKEN) String token, @RequestBody ApplyOut applyOut) throws Exception {
        return applyOutService.update(applyOut);
    }
}